package wickethub.web.components;

import java.util.Date;
import java.util.Locale;

import org.apache.wicket.util.convert.IConverter;

public class RelativeDateConverter implements IConverter {

	@Override
	public Object convertToObject(String value, Locale locale) {
		return null;
	}

	@Override
	public String convertToString(Object value, Locale locale) {
		Date date = (Date) value;
		long diff = (new Date()).getTime() - date.getTime();
		int minutes = (int) diff / 1000 / 60;
		int hours = minutes / 60;
		int days = hours / 24;
		String text = "no time";
		
		if (minutes < 1) {
			text = "less than a minute";
		} else if (minutes < 2) {
			text = "one minute";
		} else if (minutes < 29) {
			text = minutes + " minutes";
		} else if (minutes < 45) {
			text = "half an hour";
		} else if (minutes < 120) {
			text = "about an hour";
		} else if (hours < 23) {
			text = hours + " hours";
		} else if (hours < 36) {
			text = "a day";
		} else if (days < 2) {
			text = "two days";
		} else if (days < 28) {
			text = days + " days";
		} else if (days > 28 && days < 40) {
			text = "about a month";
		} else if (days > 40) {
			text = "about " + (days / 30) + "months";
		}
		
		return text + " ago";
		
	}

}
